package com.ecarx.vhaltests.hy11.testcases.tcam;

import static com.ecarx.xui.adaptapi.car.property.IProperty.SETTING_FUNC_LOCATION_INFORMATION_COLLECTION_DISPLAY;

import android.car.VehicleAreaType;
import android.car.hardware.CarPropertyValue;
import android.car.hardware.property.CarPropertyManager;

import androidx.test.platform.app.InstrumentationRegistry;

import com.ecarx.vhaltests.IpcpClient;
import com.ecarx.vhaltests.VehicleConstants;
import com.ecarx.vhaltests.VhalUnitTestBase;
import com.ecarx.xui.adaptapi.car.IWrapper;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class SettingFuncLocationInformationCollectionDisplayTest extends VhalUnitTestBase {
    private static int propertyId;
    private static IWrapper.IPropertyId wrapperProperty;
    private CarPropertyManager.CarPropertyEventCallback propertyCbk;
    private static IpcpClient ipcpClient = new IpcpClient();
    private boolean isFirstCbk;
    private static final int AREA_ID = VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL;
    private boolean cbkValue;
    private final Object cbkLock = new Object();

    @BeforeClass
    public static void testInit() {
        IWrapper wrapper = com.ecarx.xui.adaptapi.car.Car.createWrapper(InstrumentationRegistry.getInstrumentation().getTargetContext());
        wrapperProperty = wrapper.getWrappedPropertyId(IWrapper.WrappedIdType.ID_TYPE_FUNCTION, SETTING_FUNC_LOCATION_INFORMATION_COLLECTION_DISPLAY);
        propertyId = wrapperProperty.getPropertyId();
        Assert.assertTrue(isAvail());
    }

    @Test
    public void test001_GetSettingFuncLocationInformationCollectionDisplayOn() {
        testGet(true);
    }

    @Test
    public void test002_GetSettingFuncLocationInformationCollectionDisplayOff() {
        testGet(false);
    }

    @Test
    public void test003_GetSettingFuncLocationInformationCollectionDisplayCbk() {
        testCbk();
    }

    private static void mockValue(boolean value) {
        int cemSvcId = 37;
        int[] cemOperIds = {999};
        int vgmSvcId = 49;
        int[] vgmOperIds = {999};
        int timeoutSvcId = 99;
        int[] timeoutOperIds = {200};

        ipcpClient.setConfig(timeoutSvcId, timeoutOperIds);
        ipcpClient.setConfig(cemSvcId, cemOperIds);
        ipcpClient.setConfig(vgmSvcId, vgmOperIds);

        String[] timeoutConfigs = {"carLocatorActvStsTimeout"};
        int[] timeoutValues = {0};
        boolean toRet = ipcpClient.setTimeoutSignal(timeoutSvcId, timeoutOperIds[0], timeoutConfigs, timeoutValues);
        Assert.assertTrue(toRet);

        String[] cems = {"vehModMngtGlbSafe1CarModSts1", "vehModMngtGlbSafe1UsgModSts"};
        int[] cemValues = {VehicleConstants.CAR_MODE_NOMAL, VehicleConstants.USG_MODE_CONVENIENCE};
        boolean cemRet = ipcpClient.setCEMSignal(cemSvcId, cemOperIds[0], cems, cemValues);
        Assert.assertTrue(cemRet);


        String[] vgms = {"carLocatorActvSts"};
        int[] vgmValues = {value ? 1 : 2};
        boolean vgmResult = ipcpClient.setVGMSignal(vgmSvcId, vgmOperIds[0], vgms, vgmValues);
        Assert.assertTrue(vgmResult);
    }

    private void testGet(boolean value) {
        mockValue(value);
        Assert.assertEquals(value, getValue());
    }

    private boolean getValue() {
        delay();
        return mCarPropertyMgr.getBooleanProperty(propertyId, AREA_ID);
    }

    private static boolean isAvail() {
        delay();
        return mCarPropertyMgr.isPropertyAvailable(propertyId, AREA_ID);
    }

    private void testCbk() {
        cbkValue = true;
        testGet(true);
        delay();
        boolean expectedValue = false;
        propertyCbk = new CarPropertyManager.CarPropertyEventCallback() {
            @Override
            public void onChangeEvent(CarPropertyValue carPropertyValue) {
                if (carPropertyValue.getAreaId() == AREA_ID) {
                    if (isFirstCbk) {
                        isFirstCbk = false;
                    } else {
                        cbkValue = (boolean) carPropertyValue.getValue();
                        unLock();
                    }
                }
            }

            @Override
            public void onErrorEvent(int i, int i1) {
            }
        };
        mCarPropertyMgr.registerCallback(propertyCbk, propertyId, CarPropertyManager.SENSOR_RATE_ONCHANGE);
        delay();
        mockValue(expectedValue);
        lock();
        Assert.assertEquals(expectedValue, cbkValue);
    }

    private void lock() {
        try {
            synchronized (cbkLock) {
                cbkLock.wait(1000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private void unLock() {
        synchronized (cbkLock) {
            cbkLock.notify();
        }
    }

}